<?php
/**
 * Dance-Tunes API Client
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.inphpwetrust.com/dance-tunes-public-api-a-php-client-library#new-bsd-license
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to blog@inphpwetrust.com so we can send you a copy immediately.
 *
 * @category   Dancetunes
 * @package    Dancetunes_Vo
 * @copyright  Copyright (c) 2010 George Enciu  (http://www.colorsoft.ro, http://www.inphpwetrust.com)
 * @license    http://www.inphpwetrust.com/dance-tunes-public-api-a-php-client-library#new-bsd-license
 */

/**
 * @category   Dancetunes
 * @package    Dancetunes_Vo
 * @copyright  Copyright (c) 2010 George Enciu  (http://www.colorsoft.ro, http://www.inphpwetrust.com)
 * @license    http://www.inphpwetrust.com/dance-tunes-public-api-a-php-client-library#new-bsd-license
 */
class Dancetunes_Vo
{
	/**
	 * Data returned from the server in raw format
	 * 
	 * @var string
	 */
	protected $_data;
	/**
	 * Data returned from the server parsed as array
	 * 
	 * @var unknown_type
	 */
    protected $_array;
    
    /**
     * Class constructor
     */
    public function __construct($data) {
       $this->_data = $data;
       $this->_array = $this->toArray();
    }
	
    /**
     * Public function for converting the raw data into an associative array
     */
	public function toArray()
	{
		$array = array();
		$this->createArray($this->_data, $array);
		return $array;
	}
	
	/**
	 * Recursive method for parsing the SimpleXMLElement received from server.
	 * 
	 * @param SimpleXMLElement $node
	 * @param array $data
	 */
	private function createArray(SimpleXMLElement $node, array &$data)
	{
		foreach (array_keys(get_object_vars($node)) as $element)  
		{
            if (count($node->$element->children()) == 0)
            {
                $data[$element] = "{$node->$element}";
            }
            elseif (count($node->$element) > 1)
			{
				$data[$element] = array();
				foreach ($node->$element as $subnode)
				{
					$tmp = array();
	                $this->createArray($subnode, $tmp);
	                $data[$element][] = $tmp;
				}
			}
			else 
			{
                $tmp = array();
                $this->createArray($node->$element, $tmp);
                $data[$element] = $tmp;
			}
		}
	}
}
